﻿/**
 * 根据表生成对应的堆数据结构
 * 创建配置表引用类
 */
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var zlib = require('zlib');
var input = "E:\\BeautyIsNow\\data";
var output = "E:\\BeautyIsNow\\client\\src\\config";
var configManager = "configManager";
let config = "module cm {\n";
var tables = readTableCsv();
for (var i = 0; i < tables.length; i++) {
    var table = tables[i][0];
    var isArray = tables[i][1] == 1;
    var keyNames = tables[i][2].split("#");
    var names = tables[i][3];
    var json = "interface " + names + "Config {" + "\n";
    console.log("解析input:", input + "/" + table + ".csv");
    let data = fs.readFileSync(input + "/" + table + ".csv", 'utf8');
    data = data.replace(/\r/gi, "");
    var lines = data.split("\n");
    var keys = String(lines[3]).split(",");
    var types = String(lines[2]).split(",");
    var keyTypes = [];
    for (var x = 0; x < keys.length; x++) {
        if (!keys[x] || !types[x])
            continue;
        let type = "string";
        switch (types[x]) {
            case "short":
            case "byte":
            case "int":
            case "ushort":
            case "uint":
                type = "number"; break;
            case "string": type = "string"; break;
        }
        if (keyNames.indexOf(keys[x]) != -1) {
            keyTypes.push(type);
        }
        json += "    " + keys[x] + ": " + type + ";\n";
    }
    json += "}";
    fs.writeFileSync(output + "/" + names + "Config.ts", json);
    let keyStr = "";
    let lastStr = isArray ? "[]" : "";
    let isMore = keyNames && keyNames.length > 1;
    keyNames.forEach((element, index) => {
        keyStr += "[" + element + ": " + keyTypes[index] + "]" + (isMore && index < keyNames.length - 1 ? ": { " : "");
        lastStr += isMore && index < keyNames.length - 1 ? " }" : "";
    });
    config += "    export let " + names + ": { " + keyStr + ": " + names + "Config" + lastStr + " } = {};\n";
}
config += "}";
fs.writeFileSync(output + "/" + configManager + ".ts", config);
console.log("完成");
function readTableCsv() {
    var tabledata = fs.readFileSync(input + "/A_tables.csv", 'utf8');
    var lines = tabledata.split("\r\n");
    var arr = [];
    for (var i = 1; i < lines.length; i++) {
        if (lines[i] == "")
            continue;
        arr.push(lines[i].split(","));
    }
    return arr;
}